package com.socialrss.viewlisteners;

import java.util.ArrayList;

import com.socialrss.R;
import com.socialrss.accounts.Group;
import com.socialrss.utils.SocialRssModel;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class OuterCategoryViewController extends BaseActivity  implements OnItemClickListener{
	private ListView outerListView;
	private OuterListCustomAdapter listAdapter;
	private ArrayList<Group> categList;
	private View addCatButton;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  
	    setContentView(R.layout.outer_category_view); 
	    outerListView = (ListView) findViewById( R.id.outerCategoryListView ); 
	    	    
	    this.setAddCatButtonClickListener();
	    
	    // Get outer category list
	    categList = SocialRssModel.Instance(this).outerCategories();
	    
	    // Specify properties of adapters
	    listAdapter = new OuterListCustomAdapter(this,R.layout.category_list_item, categList);
	    outerListView.setAdapter( listAdapter );
	    
	    //listeners for listview 
	    outerListView.setOnItemClickListener(this);
	    this.setOnItemLongClick();
	}
	@Override
	public void onBackPressed(){
		finish();
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Group selectedCat = (Group)outerListView.getItemAtPosition(arg2);
		Intent i = new Intent(this, GroupConnections.class);
		Bundle bun = new Bundle();
		bun.putInt("categoryid", selectedCat.getId());
		i.putExtras(bun);
		startActivity(i);
	}
	/**
	 * Set Long Click Listener of ListView 
	 */
	public void setOnItemLongClick(){
		outerListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final Group item = (Group)(OuterCategoryViewController.this.outerListView).getItemAtPosition(arg2);
				AlertDialog.Builder builder1 = new AlertDialog.Builder(OuterCategoryViewController.this);
				builder1.setMessage("Are you sure to delete Group with id " + item.getId())
					.setPositiveButton("Delete", new OnClickListener() {		//ask user if he is sure about this action
						@Override
						public void onClick(DialogInterface dialog, int which) {		//if yes, do it
							SocialRssModel.Instance(null).allCategories().remove(item.getId());
							SocialRssModel.Instance(null).outerCategories().remove(item);
							SocialRssModel.Instance(null).removeGroup(item.getId());
							OuterCategoryViewController.this.listAdapter.notifyDataSetChanged();
							dialog.dismiss();
						}
					})
					.setNegativeButton("Cancel", new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
				AlertDialog mydial = builder1.create();
				mydial.show();
				
				return false;
			}
		}); 
	}
	public void setAddCatButtonClickListener(){
		addCatButton = findViewById(R.id.addNewCategoryButton);
		addCatButton.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
				View popupView = layoutInflater.inflate(R.layout.edit_text_popup, null);
				final PopupWindow editpopup = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
				//specify cancel button
				View cancelbutton = popupView.findViewById(R.id.popupcancelbutton);
				cancelbutton.setOnClickListener(new android.view.View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						editpopup.dismiss();
					}
				});
				//specify edittext
				final EditText catnameedittext = (EditText)popupView.findViewById(R.id.editTextInPopup);
				TextView title = (TextView) popupView.findViewById(R.id.editTextPopupTitle);
				
				title.setText("Create New Category");
				catnameedittext.setText("");
				catnameedittext.setHint("write a new category name");
				
				//specify ok button
				View okbutton = popupView.findViewById(R.id.popupcommitbutton);
				okbutton.setOnClickListener(new android.view.View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String name = catnameedittext.getText().toString();
						//if new group is successfully inserted
						if(SocialRssModel.Instance(null).insertNewGroup(name)){
							Group temp = new Group(SocialRssModel.Instance(null).getIdOfGroup(name), name, 0);
							SocialRssModel.Instance(null).allCategories().put(temp.getId(), temp);
							SocialRssModel.Instance(null).outerCategories().add(temp);
							listAdapter.notifyDataSetChanged();
							editpopup.dismiss();
							
						}
						catnameedittext.setText("");
						catnameedittext.setHint("Type another group name");
							
					}
				});
				
				editpopup.setFocusable(true);
				editpopup.showAsDropDown(findViewById(R.id.outerCategTextView), 0, 0);
			}
		});
	}
	private class OuterListCustomAdapter extends ArrayAdapter<Group> {
		 
		  private ArrayList<Group>  connectionList;
		 
		  public OuterListCustomAdapter(Context context, int textViewResourceId, ArrayList<Group>  connList) {
			  super(context, textViewResourceId, connList);
			  this.connectionList = connList;
		  }
		  private class ViewHolder {
			  TextView name;
			  TextView insideNum;
		  }
		  
		  @Override
		  public View getView(int position, View convertView, ViewGroup parent) {

			  ViewHolder holder = null;
		 
			  if (convertView == null) {
				  LayoutInflater vi = (LayoutInflater)(((Activity)this.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE));
				  convertView = vi.inflate(R.layout.category_list_item, null);
			 
				  holder = new ViewHolder();
				  holder.name = (TextView) convertView.findViewById(R.id.categorynameinlist);
				  holder.insideNum = (TextView) convertView.findViewById(R.id.categoryNumOfContentsInList);
				  convertView.setTag(holder);
			  } 
			  else {
				  holder = (ViewHolder) convertView.getTag();
			  }
		 
			  Group conn = connectionList.get(position);
			  holder.name.setText(conn.getName());
			  holder.insideNum.setText("Inside : "+ conn.getNumberOfElements());
		 
			  return convertView;
		  }
	}
}
